(function( $ ) {
    $.fn.ajaxForm = function(options) {
        var options = $.extend({
          callback: function(data){
            // console.log(data);
          },
          validate: function(){return true},
        }, options);

        var callback = options.callback;
        var validate = options.validate;

        this.filter( "form" ).submit(function(e) {
          var url = $(this).attr("action");
          var method = $(this).attr("method");

          if (!validate()) {
            e.preventDefault(); // avoid to execute the actual submit of the form.
            return;
          }

          $.ajax({
             type: method,
             url: url,
             data: $(this).serialize(), // serializes the form's elements.
             success: function(data) {
                 callback(data); // show response from the php script.
             }
          });
          e.preventDefault(); // avoid to execute the actual submit of the form.
        });

        return this;
    };
}( jQuery ));

(function( $ ) {
    $.fn.makeUploadFileTrigger = function() {

        this.unbind("click").click(function(e) {
          var fileId = $(this).attr("id").replace("trigger_", "");
          var containerId = fileId + "_c";
          $("#" + fileId).change(function(){
            if ($("#"+containerId).length > 0) {
              $("#"+containerId).html($(this).val());

            } else {
              $(this).after("<div id='" + containerId + "'>" + $(this).val() + "</div>");

            }
          });
          $("#" + fileId).click();

        });

        return this;
    };
}( jQuery ));

var HMUtil = {
  today: function(){
    var d = new Date();
    return "" + d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
  },
  randId: function(){
    return "r" + Math.random().toString(36).substring(2);
  },
};

var oldAlert = window.alert;
window.alert = function(msg) {
    //setup
    $.facebox.settings.closeImage = '/js/facebox/closelabel.png';
    $.facebox.settings.loadingImage = '/js/facebox/loading.gif';
    $.facebox("<div style='text-align:center;line-height: 40px;'>" + msg + "</div>");
}

function go(city_id) {
  self.location.href = "/cities/" + city_id;
}

function show_login_box() {
  $.facebox($("#facebox_loginbox").html());
  setTimeout(function(){
   //make ajax post
         $("#facebox form").ajaxForm({
           callback: function(data) {
             if (data.valid) {
               self.location.href = '/';
             } else {
               $("#codeImage").click();
               alert(data.errorMsg);
             }
           }
         });
  }, 200);
}

function updateFileTypeLink() {
  $("a").each(function(){
    var text = $(this).text();
    if (/\.pdf$/mi.test(text)) {
      $(this).addClass("filetype filetype-pdf");
    } else if (/\.(jpg|jpeg|png|gif)$/mi.test(text)) {
      $(this).addClass("filetype filetype-image");
    } else if (/\.(ppt|pptx|pps|ppsx)$/mi.test(text)) {
      $(this).addClass("filetype filetype-ppt");
    } else if (/\.(xls|xlsx)$/mi.test(text)) {
      $(this).addClass("filetype filetype-excel");
    } else if (/\.(doc|docx)$/mi.test(text)) {
      $(this).addClass("filetype filetype-word");
    }
  });
}

function makeFakeLoadingLink() {
  var loading_html = '<img src="/images/loading.gif">';
  $("a.fake-loading").each(function(){
    $(this).unbind().click(function(){
      var o = $(this);
      var html = o.html();
      if (html === loading_html) {
        return;
      }
      o.html(loading_html);
      setTimeout(function(){
        o.html(html);
      }, 2000);
    });
  });
}

function isValidPhone(phone) {
  var isMobile=/^(?:13\d|15\d|18\d)\d{5}(\d{3}|\*{3})$/; //手机号码验证规则
  var isPhone=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;  //座机验证规则
  if(!isMobile.test(phone) && !isPhone.test(phone)) {
    return false;     //返回一个错误，不向下执行
  }
  return true;
}

function makeTrExpandable(){
    $(".list table").on("click", "tr", function(e){
      if($(e.target).parents(".stopPropagation").length > 0) {
        return;
      }
      $(this).addClass("auto-height");
    });

    $(".list table").on("mouseleave", "tr", function(e){
      $(this).removeClass("auto-height");
    });
}

$(function(){
  updateFileTypeLink();
  makeFakeLoadingLink();
  makeTrExpandable();
});
